/**
 ********************************************************************
 * @author roocket
 * @file    main.c
 * @version V0.0.0
 * @date    2024/3/22
 * @brief   This file is to indicate main functions. 
 * @attention Code file rules:
 * rule: file encoding use UTF8;
 * rule: max line length 120 characters;
 * rule: line separator \r\n;
 * rule: use clion auto code format tool.
 */
#include "uart.h"

int test_serial_device(int dev_id, char *name)
{
    switch (dev_id)
    {
        case 0:
        strcpy(name, "/dev/ttyS0");
        break;

        case 1:
        strcpy(name, "/dev/ttyS1");
        break;

        case 2:
        strcpy(name, "/dev/ttyS2");
        break;

        case 3:
        strcpy(name, "/dev/ttyS3");
        break;

        case 4:
        strcpy(name, "/dev/ttyS4");
        break;

        case 5:
        strcpy(name, "/dev/ttyS5");
        break;

        case 6:
        strcpy(name, "/dev/ttyS6");
        break;

        case 7:
        strcpy(name, "/dev/ttyS7");
        break;

        case 8:
        strcpy(name, "/dev/ttyS8");
        break;

        case 9:
        strcpy(name, "/dev/ttyS9");
        break;
        case 10:
        strcpy(name, "/dev/ttyS10");
        break;

        case 11:
        strcpy(name, "/dev/ttyS11");
        break;

        default:
        return -1;
    }
    return 0;
}

void test_printf_buffer(const char* head_l, unsigned char*date_in, int length_l)
    {
    int k;

    printf("%s ", head_l);
    for (k = 0; k < length_l; k++)
        printf("%02x ", date_in[k]);
    printf("\n");
} 

void test_recv_send_pro(void *port)
{
    int ret = -1;
    char sent[32] = "helloworld";
    char recv[32] = "";
    int len;

    ret = uart_send_data(port, sent, strlen(sent));
    if (-1 == ret)
        perror("uart_send_data");

    ret = uart_recv_data(port, recv, &len);
    if (-1 == ret)
        perror("uart_recv_data");
    return;
}

int main(int argc, char **argv)
{
    int bn;
    int ret;
    char name[12];
    char sn;
    speed_t baud;
    void *port = NULL;

    if (argc < 2) {
        printf("input error:*argv=1-11\n");
        exit(-1);
    }
    if (argc > 3) {
        printf("input error: less than 4 parameters\n");
        exit(-1);
    }

    ret = sscanf(argv[1], "%hhd", &sn);

    ret = test_serial_device(sn,name);
    if (-1 == ret)
        return -1;

    ret = sscanf(argv[2], "%d", &bn);

    baud = uart_get_baud_rate(bn);
    if (-1 == ret)
        return -1;

    ret = uart_init(&port, name, baud);
    if (-1 == ret)
        return -1;

    test_recv_send_pro(port);

    return 0;
}
